package com.jilian.powerstation.modul.natives;

import android.app.Dialog;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.jilian.powerstation.MyApplication;
import com.jilian.powerstation.R;
import com.jilian.powerstation.base.BaseActivity;
import com.jilian.powerstation.common.dto.ConfigInfoDto;
import com.jilian.powerstation.common.dto.SiteSettingDto;
import com.jilian.powerstation.common.event.MessageEvent;
import com.jilian.powerstation.dialog.nicedialog.BaseNiceDialog;
import com.jilian.powerstation.dialog.nicedialog.NiceDialog;
import com.jilian.powerstation.dialog.nicedialog.ViewConvertListener;
import com.jilian.powerstation.dialog.nicedialog.ViewHolder;
import com.jilian.powerstation.http.SocketConfig;
import com.jilian.powerstation.http.SocketUtils;
import com.jilian.powerstation.utils.DataUtils;
import com.jilian.powerstation.utils.DateUtil;
import com.jilian.powerstation.utils.EmptyUtils;
import com.jilian.powerstation.utils.NumberUtils;
import com.jilian.powerstation.utils.PinziDialogUtils;
import com.jilian.powerstation.utils.RxTimerUtil;
import com.jilian.powerstation.utils.ToastUitl;
import com.jilian.powerstation.utils.rxjava.RxBus;
import com.jilian.powerstation.utils.rxjava.RxBusBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * 电站设置
 */
public class SiteSettingActivity extends BaseActivity {
    private TextView tvSiteValue;
    private View viewLint;
    private LinearLayout llSite;
    private EditText etSoc;
    private LinearLayout llOne;
    private TextView siteTitle1;
    private LinearLayout llSite1;
    private EditText etSiteValue1;
    private TextView tvSiteStartime1;
    private TextView tvSiteEndtime1;
    private LinearLayout llSite2;
    private EditText etSiteValue2;
    private TextView tvSiteStartime2;
    private TextView tvSiteEndtime2;

    //
    private TextView siteTitle2;
    private LinearLayout llSite3;

    private EditText etSiteValue3;
    private TextView tvSiteStartime3;
    private TextView tvSiteEndtime3;

    private LinearLayout llSite4;

    private EditText etSiteValue4;
    private TextView tvSiteStartime4;
    private TextView tvSiteEndtime4;

    @Override
    protected void createViewModel() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_site_setting;
    }

    @Override
    public void initView() {
        setNormalTitle("SiteSetting", v -> finish());
        setrightTitle("Submit", "#3298db", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                seiteSetting();
                // modeSetting();
            }
        });
        tvSiteValue = (TextView) findViewById(R.id.tv_site_value);
        viewLint = (View) findViewById(R.id.view_lint);
        llSite = (LinearLayout) findViewById(R.id.ll_site);
        etSoc = (EditText) findViewById(R.id.et_soc);
        llOne = (LinearLayout) findViewById(R.id.ll_one);
        siteTitle1 = (TextView) findViewById(R.id.site_title1);
        llSite1 = (LinearLayout) findViewById(R.id.ll_site1);
        etSiteValue1 = (EditText) findViewById(R.id.et_site_value1);
        tvSiteStartime1 = (TextView) findViewById(R.id.tv_site_startime1);
        tvSiteEndtime1 = (TextView) findViewById(R.id.tv_site_endtime1);
        llSite2 = (LinearLayout) findViewById(R.id.ll_site2);
        etSiteValue2 = (EditText) findViewById(R.id.et_site_value2);
        tvSiteStartime2 = (TextView) findViewById(R.id.tv_site_startime2);
        tvSiteEndtime2 = (TextView) findViewById(R.id.tv_site_endtime2);

        siteTitle2 = (TextView) findViewById(R.id.site_title2);
        llSite3 = (LinearLayout) findViewById(R.id.ll_site3);
        etSiteValue3 = (EditText) findViewById(R.id.et_site_value3);
        tvSiteStartime3 = (TextView) findViewById(R.id.tv_site_startime3);
        tvSiteEndtime3 = (TextView) findViewById(R.id.tv_site_endtime3);
        llSite4 = (LinearLayout) findViewById(R.id.ll_site4);
        etSiteValue4 = (EditText) findViewById(R.id.et_site_value4);
        tvSiteStartime4 = (TextView) findViewById(R.id.tv_site_startime4);
        tvSiteEndtime4 = (TextView) findViewById(R.id.tv_site_endtime4);
    }

    private boolean isSetting;

    /**
     * 设置
     */
    private void seiteSetting() {
        try {

            //并网截止SOC
            int AA = (int) Double.parseDouble(etSoc.getText().toString()) * 10;
            //充电功率1
            int AD = (int) Double.parseDouble(etSiteValue1.getText().toString());
            //开始时间1
            String AB = tvSiteStartime1.getText().toString();
            //结束时间1
            String AC = tvSiteEndtime1.getText().toString();
            //放电功率1
            int B0 = ((int) Double.parseDouble(etSiteValue2.getText().toString()));
            //开始时间1
            String AE = tvSiteStartime2.getText().toString();
            //结束时间1
            String AF = tvSiteEndtime2.getText().toString();


            //充电功率2
            int D5 = (int) Double.parseDouble(etSiteValue3.getText().toString());
            //开始时间2
            String D3 = tvSiteStartime3.getText().toString();
            //结束时间2
            String D4 = tvSiteEndtime3.getText().toString();
            //放电功率2
            int D8 = ((int) Double.parseDouble(etSiteValue4.getText().toString()));
            //开始时间2
            String D6 = tvSiteStartime4.getText().toString();
            //结束时间2
            String D7 = tvSiteEndtime4.getText().toString();


            Log.e(TAG, "AA: " + AA);
            Log.e(TAG, "AD: " + AD);
            Log.e(TAG, "AB: " + AB);
            Log.e(TAG, "AC: " + AC);
            Log.e(TAG, "B0: " + B0);
            Log.e(TAG, "AE: " + AE);
            Log.e(TAG, "AF: " + AF);

            Log.e(TAG, "D5: " + D5);
            Log.e(TAG, "D3: " + D3);
            Log.e(TAG, "D4: " + D4);
            Log.e(TAG, "D8: " + D8);
            Log.e(TAG, "D6: " + D6);
            Log.e(TAG, "D7: " + D7);
            getLoadingDialog().showDialog();
            isSetting = true;
            RxTimerUtil.cancel();
            RxTimerUtil.timer(16000, new RxTimerUtil.IRxNext() {
                @Override
                public void doNext() {
                    if (isSetting) {
                        getLoadingDialog().dismiss();
                        ToastUitl.showImageToastTips("Time timeout");
                    }
                }
            });
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("电站设置设置命令", "发送的16进制指令: " + DataUtils.hexBytesToString(SocketConfig.siteSetting(AA, AD, AB, AC, B0, AE, AF, D5, D3, D4, D8, D6, D7)));
                            SocketUtils.sendSerialPort(SocketConfig.siteSetting(AA, AD, AB, AC, B0, AE, AF, D5, D3, D4, D8, D6, D7));

                        }
                    }).start();

                }
            }, 10);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {

                            Log.e("电站设置--工作模式", "发送的16进制指令: " + DataUtils.hexBytesToString(SocketConfig.modeSetting(WorkingType)));
                            SocketUtils.sendSerialPort(SocketConfig.modeSetting(WorkingType));


                        }
                    }).start();

                }
            }, 1000);
        } catch (Exception e) {

        }


    }


    @Override
    public void initData() {
        EventBus.getDefault().register(this);
        initCustomTimePicker();
        sandMessage();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        MyApplication.removeActivity(this);

    }

    private int timeType;//时间类型
    private TimePickerView pvCustomTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // 当按下返回键时所执行的命令
        if (keyCode == KeyEvent.KEYCODE_BACK && isChange) {
            // 此处写你按返回键之后要执行的事件的逻辑
            showLeaveDialog();
            return false;

        }
        return super.onKeyDown(keyCode, event);

    }

    private boolean isChange;

    @Override
    public void initListener() {
        etSiteValue1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                isChange = true;
            }
        });
        etSiteValue2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                isChange = true;
            }
        });
        etSiteValue3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                isChange = true;
            }
        });
        etSiteValue4.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                isChange = true;
            }
        });
        findViewById(R.id.v_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isChange) {
                    showLeaveDialog();
                } else {
                    finish();
                }

            }
        });
        tvSiteValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSelectSettingTypeDialog();
            }
        });


        tvSiteStartime1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeType = 1;
                pvCustomTime.show();
            }
        });

        tvSiteEndtime1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeType = 2;
                pvCustomTime.show();
            }
        });


        tvSiteStartime2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeType = 3;
                pvCustomTime.show();
            }
        });

        tvSiteEndtime2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeType = 4;
                pvCustomTime.show();
            }
        });


        tvSiteStartime3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeType = 5;
                pvCustomTime.show();
            }
        });

        tvSiteEndtime3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeType = 6;
                pvCustomTime.show();
            }
        });


        tvSiteStartime4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeType = 7;
                pvCustomTime.show();
            }
        });

        tvSiteEndtime4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeType = 8;
                pvCustomTime.show();
            }
        });


    }

    private int WorkingType;

    /**
     * 选择设置配置类型对话框
     */
    private void showSelectSettingTypeDialog() {
        NiceDialog.init()
                .setLayoutId(R.layout.dialog_setting_select)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                        dialog.setOutCancel(false);


                        TextView btnOne = (TextView) holder.getView(R.id.btn_one);
                        TextView btnTwo = (TextView) holder.getView(R.id.btn_two);
                        TextView btnThree = (TextView) holder.getView(R.id.btn_three);
                        TextView btnCancel = (TextView) holder.getView(R.id.btn_cancel);

                        btnCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                            }
                        });
                        btnOne.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                WorkingType = 2;
                                dialog.dismiss();
                                initConfigView();

                            }
                        });
                        btnTwo.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                WorkingType = 3;
                                dialog.dismiss();
                                initConfigView();

                            }
                        });

                        btnThree.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                WorkingType = 4;
                                dialog.dismiss();
                                initConfigView();

                            }
                        });


                    }
                })
                .setShowBottom(true)
                .show(getSupportFragmentManager());
    }

    /**
     * 初始化设置的界面
     *
     * @param
     */
    private void initConfigView() {


        //（2:自发自用，3:错峰用电，4:应急电源）
        if (WorkingType == 2) {
            llSite.setVisibility(View.VISIBLE);
            llOne.setVisibility(View.GONE);
            tvSiteValue.setText("Self-consumption");


        }
        if (WorkingType == 3) {
            llSite.setVisibility(View.GONE);
            llOne.setVisibility(View.VISIBLE);
            tvSiteValue.setText("Peak shifting");

        }

        if (WorkingType == 4) {
            llSite.setVisibility(View.GONE);
            llOne.setVisibility(View.GONE);
            tvSiteValue.setText("Backup powe");
        }
    }


    /**
     * 初始化时间数据
     */
    private void initCustomTimePicker() {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(1918, 1, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2029, 01, 01);
        //时间选择器 ，自定义布局
        pvCustomTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                String dateStr = DateUtil.dateToString("HH:mm", date);
                isChange = true;
                switch (timeType) {
                    case 1:
                        //充电开始时间1
                        tvSiteStartime1.setText(dateStr);
                        break;
                    case 2:
                        //充电结束时间1
                        tvSiteEndtime1.setText(dateStr);
                        break;
                    case 3:
                        //放电开始时间1
                        tvSiteStartime2.setText(dateStr);
                        break;
                    case 4:
                        //放电结束时间1
                        tvSiteEndtime2.setText(dateStr);
                        break;
                    case 5:
                        //充电开始时间2
                        tvSiteStartime3.setText(dateStr);
                        break;
                    case 6:
                        //充电结束时间2
                        tvSiteEndtime3.setText(dateStr);
                        break;
                    case 7:
                        //放电开始时间2
                        tvSiteStartime4.setText(dateStr);
                        break;
                    case 8:
                        //放电结束时间2
                        tvSiteEndtime4.setText(dateStr);
                        break;
                }

            }
        })
                /*.setType(TimePickerView.Type.ALL)//default is all
                .setCancelText("Cancel")
                .setSubmitText("Sure")
                .setContentTextSize(18)
                .setTitleSize(20)
                .setTitleText("Title")
                .setTitleColor(Color.BLACK)
               /*.setDividerColor(Color.WHITE)//设置分割线的颜色
                .setTextColorCenter(Color.LTGRAY)//设置选中项的颜色
                .setLineSpacingMultiplier(1.6f)//设置两横线之间的间隔倍数
                .setTitleBgColor(Color.DKGRAY)//标题背景颜色 Night mode
                .setBgColor(Color.BLACK)//滚轮背景颜色 Night mode
                .setSubmitColor(Color.WHITE)
                .setCancelColor(Color.WHITE)*/
                /*.animGravity(Gravity.RIGHT)// default is center*/
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_hour_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        final TextView tvCancel = (TextView) v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                    }
                })
                .setContentTextSize(18)
                //各个部分是否显示
                .setType(new boolean[]{false, false, false, true, true, false})
                //时间格式
                .setLabel("", "", "", " ", " ", " ")
                .setLineSpacingMultiplier(1.2f)
                .setTextXOffset(40, 0, -40, 0, 0, -0)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(0xFFe0e0e0)
                .setLineSpacingMultiplier(2f)
                .build();
    }


    private void sandMessage() {
        getLoadingDialog().show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("电站设置查询命令", "发送的16进制指令: " + DataUtils.hexBytesToString(SocketConfig.getSiteSetting()));
                        SocketUtils.sendSerialPort(SocketConfig.getSiteSetting());
                    }
                }).start();

            }
        }, 10);
    }


    //熊浪添加RxBus工具类，页面销毁时需要取消注册，否则会有内存泄漏
    private boolean isGetData;
    private String errors[] = new String[]{"Configuration is complete", "After finishing the configuration, power down and restart the device.", "After finishing the configuration, restart in operating mode.", "Disable services for system configuration", "Unable to configure in transparent transmission upgrade", "Parameter out of range", "Parameter ID is not defined", "Parameter conflict, please check"};


    /**
     * 显示电站设置信息
     *
     * @param settingDto
     */
    private void initSiteSetting(SiteSettingDto settingDto) {
        Log.e(TAG, "电站设置查询命令: 已经查询到数据，去掉重复查询");
        isGetData = true;

        Log.e(TAG, "电站设置--: 并网截止SOC" + settingDto.getAA() / 10);
        etSoc.setText(settingDto.getAA() / 10 + "");

        //充电1
        Log.e(TAG, "电站设置--: 充电功率1" + settingDto.getAD());
        etSiteValue1.setText(settingDto.getAD() + "");

        Log.e(TAG, "电站设置--: 开始时间1" + settingDto.getAB());
        tvSiteStartime1.setText(settingDto.getAB());

        Log.e(TAG, "电站设置--: 结束时间1" + settingDto.getAC());
        tvSiteEndtime1.setText(settingDto.getAC());


        //放电1
        Log.e(TAG, "电站设置--: 放电功率1" + settingDto.getB0());
        etSiteValue2.setText(settingDto.getB0() + "");

        Log.e(TAG, "电站设置--: 开始时间2" + settingDto.getAE());
        tvSiteStartime2.setText(settingDto.getAE());


        Log.e(TAG, "电站设置--: 结束时间2" + settingDto.getAF());
        tvSiteEndtime2.setText(settingDto.getAF());


        //充电2
        Log.e(TAG, "电站设置--: 充电功率2" + settingDto.getD5());
        etSiteValue3.setText(settingDto.getD5() + "");

        Log.e(TAG, "电站设置--: 开始时间3" + settingDto.getD3());
        tvSiteStartime3.setText(settingDto.getD3());

        Log.e(TAG, "电站设置: 结束时间3" + settingDto.getD4());
        tvSiteEndtime3.setText(settingDto.getD4());


        //放电2
        Log.e(TAG, "电站设置--: 放电功率3" + settingDto.getD8());
        etSiteValue4.setText(settingDto.getD8() + "");

        Log.e(TAG, "电站设置--: 开始时间4" + settingDto.getD6());
        tvSiteStartime4.setText(settingDto.getD6());


        Log.e(TAG, "电站设置--: 结束时间4" + settingDto.getD7());
        tvSiteEndtime4.setText(settingDto.getD7());
        isChange = false;

    }

    private String tip;

    /**
     * 退出登录对话框
     */
    private void showTipsDialog(String msg) {
        isChange = false;
        Dialog dialog = PinziDialogUtils.getDialogNotTouchOutside(this, R.layout.dialog_confirm);
        TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
        TextView tvContent = (TextView) dialog.findViewById(R.id.tv_content);
        TextView tvNo = (TextView) dialog.findViewById(R.id.tv_no);
        TextView tvOk = (TextView) dialog.findViewById(R.id.tv_ok);
        tvTitle.setText("tips");
        tvContent.setText(msg);
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();

            }
        });
        tvNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();


    }

    /**
     * //监听外来是否要去成功的界面
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent event) {
        /* Do something */
        if (EmptyUtils.isNotEmpty(event)
                && EmptyUtils.isNotEmpty(event.getSendMessage())

        ) {

            initSenMessage(event.getSendMessage());
        }
    }

    private boolean isFlag;

    // private String[] modes = new String[]{"Power-up", "Stand-by", "Self-consumption ", "Peak shifting", "Backup power", "Manual mode", "Error", "Testing mode"};
    private void initSenMessage(RxBusBean.SendMessage sendMessage) {

        if (DataUtils.HexToInt(sendMessage.info[6]) == 0x94) {//EMS信息上传(0x94)
            if (isFlag) {
                return;
            }
            WorkingType = DataUtils.HexToInt(sendMessage.info[12]);
            Log.e(TAG, "当前系统工作模式: " + WorkingType);
            isFlag = true;
            initConfigView();



        }

        //  查询命令
        else if (sendMessage != null && sendMessage.readContent[2] == SocketConfig.SEARCH) {
            try {
                getLoadingDialog().dismiss();
                Log.e("电站设置查询命令", "我接收到了查询命令的所有数据(已过滤传输规则中其他数据):" + Arrays.toString(sendMessage.info));
                Log.e("电站设置查询命令", "前六位是年月日时分秒:" + DataUtils.HexToInt(sendMessage.info[0]) + "年" + DataUtils.HexToInt(sendMessage.info[1]) + "月" + DataUtils.HexToInt(sendMessage.info[2]) + "日" + DataUtils.HexToInt(sendMessage.info[3]) + "时" + DataUtils.HexToInt(sendMessage.info[4]) + "分" + DataUtils.HexToInt(sendMessage.info[5]) + "秒");

                List<String> infoList = Arrays.asList(sendMessage.info);

                int AAIndex = infoList.indexOf("AA");
                int AA = DataUtils.HexToInt(sendMessage.info[AAIndex+2] + sendMessage.info[AAIndex+1]);
                Log.e("电站设置查询命令", "并网截止SOC: " + AA);


                /////////111111
                int ADIndex = infoList.indexOf("AD");
                int AD = DataUtils.HexToInt(sendMessage.info[ADIndex+2] + sendMessage.info[ADIndex+1]);
                Log.e("电站设置查询命令", "充电功率1: " + AD);


                int ABIndex = infoList.indexOf("AB");
                String AB = DataUtils.sixToten(sendMessage.info[ABIndex+1]) + ":" + DataUtils.sixToten(sendMessage.info[ABIndex+2]);
                Log.e("电站设置查询命令", "开始时间1: " + AB);


                int ACIndex = infoList.indexOf("AC");
                String AC = DataUtils.sixToten(sendMessage.info[ACIndex+1]) + ":" + DataUtils.sixToten(sendMessage.info[ACIndex+2]);
                Log.e("电站设置查询命令", "结束时间1: " + AC);


                int B0Index = infoList.indexOf("B0");
                int B0 = DataUtils.HexToInt(sendMessage.info[B0Index+2] + sendMessage.info[B0Index+1]);
                Log.e("电站设置查询命令", "放电功率1: " + B0);

                int AEIndex = infoList.indexOf("AE");
                String AE = DataUtils.sixToten(sendMessage.info[AEIndex+1]) + ":" + DataUtils.sixToten(sendMessage.info[AEIndex+2]);
                Log.e("电站设置查询命令", "开始时间2: " + AE);


                int AFIndex = infoList.indexOf("AF");
                String AF = DataUtils.sixToten(sendMessage.info[AFIndex+1]) + ":" + DataUtils.sixToten(sendMessage.info[AFIndex+2]);
                Log.e("电站设置查询命令", "结束时间2: " + AF);


                ///222222222
                int D5Index = infoList.indexOf("D5");
                int D5 = DataUtils.HexToInt(sendMessage.info[D5Index+2] + sendMessage.info[D5Index+1]);
                Log.e("电站设置查询命令", "充电功率2: " + D5);

                int D3Index = infoList.indexOf("D3");
                String D3 = DataUtils.sixToten(sendMessage.info[D3Index+1]) + ":" + DataUtils.sixToten(sendMessage.info[D3Index+2]);
                Log.e("电站设置查询命令", "开始时间3: " + D3);


                int D4Index = infoList.indexOf("D4");
                String D4 = DataUtils.sixToten(sendMessage.info[D4Index+1]) + ":" + DataUtils.sixToten(sendMessage.info[D4Index+2]);
                Log.e("电站设置查询命令", "结束时间3: " + D4);


                int D8Index = infoList.indexOf("D8");
                int D8 = DataUtils.HexToInt(sendMessage.info[D8Index+2] + sendMessage.info[D8Index+1]);
                Log.e("电站设置查询命令", "放电功率2: " + D8);

                int D6Index = infoList.indexOf("D6");
                String D6 = DataUtils.sixToten(sendMessage.info[D6Index+1]) + ":" + DataUtils.sixToten(sendMessage.info[D6Index+2]);
                Log.e("电站设置置查询命令", "开始时间4: " + D6);

                int D7Index = infoList.indexOf("D7");
                String D7 = DataUtils.sixToten(sendMessage.info[D7Index+1]) + ":" + DataUtils.sixToten(sendMessage.info[D7Index+2]);
                Log.e("电站设置置查询命令", "结束时间4: " + D7);


                SiteSettingDto settingDto = new SiteSettingDto(AA, AD, AB, AC, B0, AE, AF, D5, D3, D4, D8, D6, D7);
                initSiteSetting(settingDto);
            }

            catch (Exception e){
                ToastUitl.showImageToastSuccess(e.getMessage()+" - ");

            }

        }

        //  设置命令
        else if (sendMessage != null && sendMessage.readContent[2] == SocketConfig.SETTING) {
            isSetting = false;
            getLoadingDialog().dismiss();
            String hexString = DataUtils.ByteArrToHex(sendMessage.readContent);
            Log.e(TAG, "电站设置设置命令总的数据:" + hexString);
            Log.e("电站设置设置命令", "我接收到了实时信息的所有数据(已过滤传输规则中其他数据):" + Arrays.toString(sendMessage.info));
            Log.e("电站设置设置命令", "前六位是年月日时分秒:" + DataUtils.HexToInt(sendMessage.info[0]) + "年" + DataUtils.HexToInt(sendMessage.info[1]) + "月" + DataUtils.HexToInt(sendMessage.info[2]) + "日" + DataUtils.HexToInt(sendMessage.info[3]) + "时" + DataUtils.HexToInt(sendMessage.info[4]) + "分" + DataUtils.HexToInt(sendMessage.info[5]) + "秒");
            int result = DataUtils.HexToInt(sendMessage.info[6]);
            Log.e(TAG, "电站设置设置命令返回结果: " + result);

            if (result == 0 || result == 1 || result == 2 || result == 3 || result == 4 || result == 6) {
                tip = errors[result];
                showTipsDialog(tip);
            } else {
                if (result == 5) {
                    int id = DataUtils.HexToInt(sendMessage.info[7]);
                    double min;
                    double max;
                    //16进制ID
                    String idStr = DataUtils.toHexString(id);
                    if (
                            "aa".equals(idStr) || "AA".equals(idStr)) {
                        min = DataUtils.HexToInt(sendMessage.info[9] + sendMessage.info[8]) * 0.1;
                        max = DataUtils.HexToInt(sendMessage.info[11] + sendMessage.info[10]) * 0.1;

                    } else {
                        min = DataUtils.HexToInt(sendMessage.info[9] + sendMessage.info[8]);
                        max = DataUtils.HexToInt(sendMessage.info[11] + sendMessage.info[10]);
                    }
                    if ("aa".equals(idStr) || "AA".equals(idStr)) {
                        tip = "Self use cutoff SOC is between " + NumberUtils.forNormalMatNumber(min) + " and " + NumberUtils.forNormalMatNumber(max);
                    }
                    else{
                        //D8  D5 B0 AD
                        if("D8".equals(idStr)||"d8".equals(idStr)){
                            tip = "Discharge power  " + NumberUtils.forNormalMatNumber(min) + " and " + NumberUtils.forNormalMatNumber(max);
                        }
                        if("D5".equals(idStr)||"d5".equals(idStr)){
                            tip = "Charging power  " + NumberUtils.forNormalMatNumber(min) + " and " + NumberUtils.forNormalMatNumber(max);
                        }
                        if("B0".equals(idStr)||"b0".equals(idStr)){
                            tip = "Discharge power  " + NumberUtils.forNormalMatNumber(min) + " and " + NumberUtils.forNormalMatNumber(max);
                        }
                        if("AD".equals(idStr)||"ad".equals(idStr)){
                            tip = "Charging power  " + NumberUtils.forNormalMatNumber(min) + " and " + NumberUtils.forNormalMatNumber(max);
                        }

                    }
//

                    showTipsDialog(tip);

                } else {
                    tip = "System abnormal, error code is" + sendMessage.info[6];
                    showTipsDialog(tip);
                }
            }


        }

    }

}
